package FilterChain知识;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
@WebFilter(urlPatterns = "/demo/*")
public class AuthenticationFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) {
        System.out.println("AuthenticationFilter 初始化");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        Cookie[] cookies = httpRequest.getCookies();
        System.out.println(cookies);
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println("Found cookie: " + cookie.getName() + "=" + cookie.getValue());
            }
        }

        // 继续执行过滤器链
        chain.doFilter(request, response);
    }


    @Override
    public void destroy() {
        System.out.println("AuthenticationFilter 销毁");
    }
}

